package edu.qqilin.springframework.context.support;

import edu.qqilin.springframework.beans.BeansException;
import edu.qqilin.springframework.beans.factory.ConfigurableListableBeanFactory;
import edu.qqilin.springframework.beans.factory.config.BeanFactoryPostProcessor;
import edu.qqilin.springframework.beans.factory.config.BeanPostProcessor;
import edu.qqilin.springframework.context.ConfigurableApplicationContext;
import edu.qqilin.springframework.core.io.DefaultResourceLoader;

import java.util.Map;

/**
 * Description: 容器启动流程的总导演
 * <p>
 * 核心功能：
 * <p>
 * 实现了 refresh() 方法，定义了标准的启动流程（加载定义 -> 激活 BFPP -> 注册 BPP -> 实例化单例）。
 * <p>
 * 模板方法： 它不负责具体的 BeanFactory 创建和 XML 加载，这些留给子类去实现。
 * <p>
 * 自动化： 负责自动扫描并调用 BeanFactoryPostProcessor 和 BeanPostProcessor。
 *
 * @author qqilin1213
 * @version 1.0
 * @date 2025/11/24 20:14
 */
public abstract class AbstractApplicationContext extends DefaultResourceLoader implements ConfigurableApplicationContext {
    @Override
    public void refresh() throws BeansException {
        // 1. 创建 BeanFactory，并加载 BeanDefinition
        refreshBeanFactory();
        // 2. 获取 BeanFactory
        ConfigurableListableBeanFactory beanFactory = getBeanFactory();
        // 3. 在 Bean 实例化之前，执行 BeanFactoryPostProcessor
        // (Invoke factory processors registered as beans in the context.)
        invokeBeanFactoryPostProcessors(beanFactory);
        // 4. BeanPostProcessor 需要提前于其他 Bean 对象实例化之前执行注册操作
        registerBeanPostProcessors(beanFactory);
        // 5. 提前实例化单例Bean对象
        beanFactory.preInstantiateSingletons();
    }

    private void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) {
        Map<String, BeanFactoryPostProcessor> beanFactoryPostProcessorMap = beanFactory.getBeansOfType(BeanFactoryPostProcessor.class);
        for (BeanFactoryPostProcessor beanFactoryPostProcessor : beanFactoryPostProcessorMap.values()) {
            beanFactoryPostProcessor.postProcessBeanFactory(beanFactory);
        }
    }

    private void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory) {
        Map<String, BeanPostProcessor> beanPostProcessorMap = beanFactory.getBeansOfType(BeanPostProcessor.class);
        for (BeanPostProcessor beanPostProcessor : beanPostProcessorMap.values()) {
            beanFactory.addBeanPostProcessor(beanPostProcessor);
        }
    }

    protected abstract void refreshBeanFactory() throws BeansException;

    protected abstract ConfigurableListableBeanFactory getBeanFactory();

    @Override
    public Object getBean(String name) throws BeansException {
        return getBeanFactory().getBean(name);
    }

    @Override
    public Object getBean(String name, Object... args) throws BeansException {
        return getBeanFactory().getBean(name, args);
    }

    @Override
    public <T> T getBean(String name, Class<T> requiredType) throws BeansException {
        return getBeanFactory().getBean(name, requiredType);
    }

    @Override
    public <T> Map<String, T> getBeansOfType(Class<T> type) throws BeansException {
        return getBeanFactory().getBeansOfType(type);
    }

    @Override
    public String[] getBeanDefinitionNames() {
        return getBeanFactory().getBeanDefinitionNames();
    }
}
